//
//  ViewController+search.swift
//  WZApp
//
//  Created by evol on 2018/7/9.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit

extension ViewController {
    
    func setSearchButton(){
        //搜索
        searchButton = UIButton(type: .custom)
        searchButton.setImage(UIImage(named: "icon_search"), for: .normal)
        view.addSubview(searchButton)
        searchButton.snp.makeConstraints { (make) in
            make.top.equalTo(STATUSBAR_HEIGHT)
            make.right.equalTo(view).offset(-15)
        }
        
        searchButton.rx.tap.subscribe(onNext:{
            [weak self] _ in
            self?.searchButton.isEnabled = false
            
            let searchViewModel = ELSearchViewModel()
            let searchView = ELSearchView(searchViewModel,searchButton:(self?.searchButton)!)
            self?.view.insertSubview(searchView, belowSubview: (self?.searchButton)!)
            searchView.snp.makeConstraints({ (make) in
                make.edges.equalTo((self?.view)!)
            })
            
            UIView .animate(withDuration: 0.25, animations: {
                self?.searchButton.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
            })
            //关闭搜索视图
            searchViewModel.closeCommand.subscribe(onNext: { (result) in
                UIView.animate(withDuration: 0.25, animations: {
                    self?.searchButton.transform = CGAffineTransform(scaleX: 1, y: 1)
                }, completion: { (completed) in
                    self?.searchButton.isHidden = result
                    self?.searchButton.isEnabled = true
                })
            }).disposed(by: (self?.disposeBag)!)
            
            searchViewModel.locationCommand.subscribe(onNext: {
                (model) in
                
            }).disposed(by: (self?.disposeBag)!)
            
        }).disposed(by: disposeBag)
        
    }
}
